package it.rossignoli.enrico.app.Tesi;


import java.util.List;

import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;


//Activity list che mi servira per gestire il training e l invio dei gesti sulla sib

//integro nella classe il servizio gesti
import de.dfki.ccaal.gestures.IGestureRecognitionListener;
import de.dfki.ccaal.gestures.IGestureRecognitionService;
import de.dfki.ccaal.gestures.classifier.Distribution;


public class ListTraining extends ListActivity 
{
	
	//Classe di appoggio per il servizio gesti
	IGestureRecognitionService recognitionService;
	
	String[] values = new String[] { "MoveUp","MoveDown","MoveLeft","MoveRight"};
	
	int count=0;
	//metodi necessari al servizio gesti
	//creazione della connessione
	private final ServiceConnection serviceConnection = new ServiceConnection() 
	{

		
		public void onServiceConnected(ComponentName className, IBinder service) 
		{
			recognitionService = IGestureRecognitionService.Stub.asInterface(service);
			try 
			{
				//Faccio partire in modalita riconoscimento e attivo pure il recorder
				//recognitionService.startClassificationMode("default");
				//Registro il listener in maniera tale da poter recepire le informazioni inviate dal servizio sfruttando i metodi dell interfaccia listener
				//Posso quindi usare i metodi implementati nello stub di questa activity

				recognitionService.registerListener(IGestureRecognitionListener.Stub.asInterface(gestureListenerStub));
				
				List<String> Item= recognitionService.getGestureList("default");
				MyArrayAdapter adapter = new MyArrayAdapter(ListTraining.this,values, Item);
				
				setListAdapter(adapter);
			} 
			catch (RemoteException e1) 
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

		
		public void onServiceDisconnected(ComponentName className) 
		{
			recognitionService = null;
		}
	};

	protected boolean imparato;

	//implementazione dei metodi di ritorno dello stub
	IBinder gestureListenerStub = new IGestureRecognitionListener.Stub() 
	{

		
		public void onGestureLearned(final String gestureName) throws RemoteException 
		{
			
			System.err.println("Gesture"+gestureName +" learned");
			count++;
			imparato=false;
			if(count!=3)
			{
				runOnUiThread(new Runnable() 
				{
					
					public void run() 
					{
						Toast.makeText(ListTraining.this, gestureName+":"+count, Toast.LENGTH_LONG).show();
						
					}
				});
			}
			else
			{
				imparato=true;
				runOnUiThread(new Runnable() 
				{
					
					public void run() 
					{
						Toast.makeText(ListTraining.this, String.format("%s imparato",gestureName), Toast.LENGTH_LONG).show();
						
					}
				});
				
				refreshList();
			}

		}

		
		public void onTrainingSetDeleted(String trainingSet) throws RemoteException 
		{
//			Toast.makeText(ProvaTrainingActivity.this, String.format("Training set %s deleted", trainingSet), Toast.LENGTH_SHORT).show();
//			System.err.println(String.format("Training set %s deleted", trainingSet));
		}

		
		public void onGestureRecognized(final Distribution distribution) throws RemoteException 
		{
			runOnUiThread(new Runnable() 
			{
				
				public void run() 
				{
//					Toast.makeText(ProvaTrainingActivity.this, String.format("%s: %f", distribution.getBestMatch(), distribution.getBestDistance()), Toast.LENGTH_LONG).show();
//					System.err.println(String.format("%s: %f", distribution.getBestMatch(), distribution.getBestDistance()));
				}
			});
		}
	};
	
	
	public void onCreate(Bundle icicle) 
	{
		super.onCreate(icicle);


	}

	protected void refreshList() 
	{
		// TODO Auto-generated method stub
		List<String> Item;
		try {
			Item = recognitionService.getGestureList("default");
			MyArrayAdapter adapter = new MyArrayAdapter(ListTraining.this,values, Item);
			
			setListAdapter(adapter);
			if(count==3)
			{
				count=0;
				recognitionService.stopLearnMode();
			}
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) 
	{
		String item = (String) getListAdapter().getItem(position);
		
		//gestire il training 
		
		if (recognitionService != null) {
			try {
				if (true) 
				{

					recognitionService.startLearnMode("default",item);
				} else {

					recognitionService.stopLearnMode();
				}
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		Toast.makeText(this, item + " Training numero:"+count, Toast.LENGTH_LONG).show();
	}
	
		
	protected void onRestart() 
	{
		super.onRestart();
		Log.i("ActivityDemo", "Richiamato onRestart()");
	}
	
	@Override
	protected void onStart() 
	{
		super.onStart();
		Log.i("ActivityDemo", "Richiamato onStart()");
	}
	
	@Override
	protected void onResume() 
	{
		//creo la connessione vera al servizio gesti
		Log.i("ActivityDemo", "Richiamato onResume()");
		Intent bindIntent = new Intent("de.dfki.ccaal.gestures.GESTURE_RECOGNIZER");
		bindService(bindIntent, serviceConnection, Context.BIND_AUTO_CREATE);
		
			//List<String> Item= recognitionService.getGestureList("default");

		super.onResume();
	
		
		
	}
	
	@Override
	protected void onPause() 
	{
		
		Log.i("ActivityDemo", "Richiamato onPause()");
		//Disconnetto il listener ai metodi dello stub
		try 
		{
			recognitionService.unregisterListener(IGestureRecognitionListener.Stub.asInterface(gestureListenerStub));
		} catch (RemoteException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		recognitionService = null;
		//mi disconetto dal servizio
		unbindService(serviceConnection);
		super.onPause();
	}
	
	@Override
	protected void onStop() 
	{
		super.onStop();
		Log.i("ActivityDemo", "Richiamato onStop()");
	}
	
	@Override
	protected void onDestroy() 
	{
		super.onDestroy();
		Log.i("ActivityDemo", "Richiamato onDestroy()");
	}
	
	
	
	
	
}